package com.atguigu.yygh.common.handler;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author luoyin
 * @Date 16:11 2022/9/20
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R error(Exception e){
        e.printStackTrace();
        return R.error().message("异常!");
    }

    @ExceptionHandler(ArithmeticException.class)
    public R errorArithmeticException(ArithmeticException e){
        //e.printStackTrace();
        return R.error().message("数学异常!");
    }
    @ExceptionHandler(RuntimeException.class)
    public R errorRuntimeException(RuntimeException e){
        //e.printStackTrace();
        return R.error().message("运行时异常!");
    }
    @ExceptionHandler(SQLException.class)
    public R errorSQLException(SQLException e){
        //e.printStackTrace();
        return R.error().message("sql语句异常!");
    }
    @ExceptionHandler(IOException.class)
    public R errorIOException(IOException e){
        //e.printStackTrace();
        return R.error().message("io异常!");
    }

    @ExceptionHandler(YyghException.class)
    public R errorYyghException(YyghException e){
        return R.error().message(e.getMessage()).code(e.getCode());
    }

}
